const util = require('../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wenzhangList: [],
    currPage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getWenZhagnData()
  },

  onShareAppMessage: function () {
    return {
      title: '前端历程',
      path: '/pages/home/index',
      imageUrl: '/lib/images/banner1.png'
    }
  },

  // 页面下拉刷新
  onPullDownRefresh: function() {
    this.setData({
      currPage: 1,
      wenzhangList: []
    })
    this.getWenZhagnData()
    wx.stopPullDownRefresh()
  },

  // 上拉刷新事件
  onReachBottom: function() {
    this.setData({
      currPage: this.data.currPage + 1
    })
    this.getWenZhagnData()
  },

  getWenZhagnData() {
    wx.showLoading()
    wx.request({
      url: 'https://ku.qingnian8.com/wxList.php?num=10&cid=14',
      data: {
        page: this.data.currPage,
        loadState: false
      },
      success: (res) => {
        wx.hideLoading()
        res.data.map(item => {
          item.posttime = util.formatTime(new Date(item.posttime*1000))
          if(item.title.length > 12) {
            item.title = `${item.title.slice(0, 12)}...`
          }
          return item
        })
        if(res.data.length === 0) {
          this.setData({
            "loadState": true
          })
          return false
        }
        this.setData({
          wenzhangList: [...this.data.wenzhangList, ...res.data]
        })
      }
    })
  },
})